package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.StockOut;
import com.ericom.service.GenericManager;

public class StockOutAction extends BaseAction {
	private GenericManager<StockOut, Long> stockOutManager;
	private List stockOuts;
	private StockOut stockOut;
	private Long id;

	public StockOut getStockOut() {
		return stockOut;
	}

	public void setStockOut(StockOut stockOut) {
		this.stockOut = stockOut;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setStockOutManager(
			GenericManager<StockOut, Long> stockOutManager) {
		this.stockOutManager = stockOutManager;
	}

	public String list() {
		stockOuts = stockOutManager.getAll();
		return SUCCESS;
	}

	public List getStockOuts() {
		return stockOuts;
	}

	public String delete() {
		stockOutManager.remove(stockOut.getId());
		this.saveMessage(getText("stockOut.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			stockOut = stockOutManager.get(id);
		} else {
			stockOut = new StockOut();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (stockOut.getId() == null);
		stockOut = stockOutManager.save(stockOut);

		String key = (isNew) ? "stockOut.added" : "stockOut.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}

}
